package br.com.felinelayer.client.clientSwing;

import br.com.felinelayer.client.IComponentClient;
import br.com.felinelayer.common.Message;
import br.com.felinelayer.common.Protocol;
import java.awt.Container;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 */
public class FrameClient extends ComponentSwing<FrameClient, JFrame> implements br.com.felinelayer.client.IFrameClient, WindowListener {

    /** a */
    private JFrame jframe;
    /** a */
    private Container contentPane;
    /** a */
    private ArrayList<br.com.felinelayer.client.IComponentClient> components = new ArrayList<>();
    /** a */
    private boolean inRemoveComponent;

    @Override
    protected void doInitialize() {
        // Create the frame
        String title = "Frame Title";
        jframe = new JFrame( title );
        //        // Create a component to add to the frame
        //        JComponent comp = new JTextArea();
        //        // Add the component to the frame's content pane;
        //        // by default, the content pane has a border layout
        //        frame.getContentPane().add( comp, BorderLayout.CENTER );
        // Show the frame
        int width = 600;
        int height = 300;
        // 
        jframe.addWindowListener( this );
        // 
        contentPane = jframe.getContentPane();
        contentPane.setLayout( null );
        jframe.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
        jframe.setSize( width, height );
        jframe.setVisible( false );
    }

    @Override
    public void addComponent( br.com.felinelayer.client.IComponentClient component ) {
        components.add( component );
        // 
        if ( component.getObject() instanceof java.awt.Component ) {
            java.awt.Component o = (java.awt.Component) component.getObject();
            contentPane.add( o );
            //rowPanel.add( o );
        }
    }

    @Override
    public Object getObject() {
        return jframe;
    }

    @Override
    public FrameClient setText( String text ) {
        jframe.setTitle( text );
        return this;
    }

    @Override
    public void close() {
        jframe.setVisible( false );
    }

    @Override
    public void windowClosing( WindowEvent e ) {
        Runnable runner = new Runnable() {
            @Override
            public void run() {
                try {
                    Message message = createEvent( Protocol.EVENT_BEFORE_CLOSE );
                    // Defines default
                    message.setValueByName( Protocol.EVENT_BEFORE_CLOSE_CANCLOSE, Protocol.YES );
                    message = fireInquireEvent( message );
                    if ( message.getValueByName( Protocol.EVENT_BEFORE_CLOSE_CANCLOSE ).equals(
                            Protocol.YES ) ) {
                        SwingUtilities.invokeLater( new Runnable() {
                            @Override
                            public void run() {
                                jframe.setVisible( false );
                                destroy();
                            }
                        } );
                        //System.exit( 0 );
                    }
                } catch ( Exception ex ) {
                    Logger.getLogger( FrameClient.class.getName() ).log( Level.SEVERE, null, ex );
                }
            }
        };
        Thread thread = new Thread( runner );
        thread.setName( "windowClosing" );
        thread.start();
    }

    @Override
    public void windowOpened( WindowEvent e ) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void windowClosed( WindowEvent e ) {
        destroy();
    }

    @Override
    public void windowIconified( WindowEvent e ) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void windowDeiconified( WindowEvent e ) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void windowActivated( WindowEvent e ) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void windowDeactivated( WindowEvent e ) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public String getText() {
        return jframe.getTitle();
    }

    @Override
    public void messageReceived( Message message ) {
        if ( message.getName().equalsIgnoreCase( Protocol.MSG_NAME_METHOD ) ) {
            String method = message.getValueByName( Protocol.METHOD_NAME );
            if ( method.equalsIgnoreCase( Protocol.METHOD_FRAME_SHOW ) ) {
                jframe.setVisible( true );
            }
            if ( method.equalsIgnoreCase( Protocol.METHOD_FRAME_CLOSE ) ) {
                jframe.setVisible( false );
            }
        }
    }

    @Override
    public void removeComponent( IComponentClient component ) {
        if ( inRemoveComponent ) {
            return;
        }
        inRemoveComponent = true;
        components.remove( component );
        component.destroy();
        inRemoveComponent = false;
    }

//    public Collection<IComponentClient> getComponents() {
//        return components;
//    }
    @Override
    public void doDestroy() {
        while ( components.size() > 0 ) {
            components.get( 0 ).destroy();
        }
    }
}
